package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketCoupon;
import com.cskaoyan.market.db.domain.MarketCouponUser;
import com.cskaoyan.market.service.AdminCouponService;
import com.cskaoyan.market.service.impl.AdminCouponServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/admin/coupon/*")
public class AdminCouponController extends HttpServlet {
    AdminCouponService adminCouponService=new AdminCouponServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
        if("list".equals(op)){
            list(req, resp);
        }else if("listuser".equals(op)){
            listuser(req,resp);
        }
    }

    private void listuser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String couponId = req.getParameter("couponId");
        String status = req.getParameter("status");
        List<MarketCouponUser> couponUserList = adminCouponService.listuser(page, limit, sort, order,couponId,status);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(couponUserList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String sort = req.getParameter("sort");
        String order = req.getParameter("order");
        String name = req.getParameter("name");
        String type = req.getParameter("type");
        String status = req.getParameter("status");
        List<MarketCoupon> couponList = adminCouponService.list(page, limit, sort, order,name, type,status);
        //对于分页查询的这些接口，那么我们需要调用oKlIST方法
        Object o = ResponseUtil.okList(couponList);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String op = requestURI.replace(req.getContextPath() + "/admin/coupon/", "");
        if("create".equals(op)){
            create(req, resp);
        }else if("delete".equals(op)){
            delete(req,resp);
        }else if("update".equals(op)){
            update(req,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        ObjectMapper mapper = JacksonUtil.getObjectMapper();
        MarketCoupon marketCoupon = mapper.readValue(requestBody, MarketCoupon.class);
        //String daysType = JacksonUtil.parseString(requestBody, "daysType");
        // MarketCoupon marketCoupon = objectMapper.readValue(requestBody, MarketCoupon.class);
        int i = adminCouponService.update(marketCoupon);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        Integer id= JacksonUtil.parseInteger(requestBody,"id");
        int i=adminCouponService.delete(id);
        Object o = ResponseUtil.ok(i);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }

    private void create(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String requestBody = req.getReader().readLine();
        ObjectMapper mapper = JacksonUtil.getObjectMapper();
        MarketCoupon marketCoupon = mapper.readValue(requestBody, MarketCoupon.class);
        MarketCoupon marketCoupon1 = adminCouponService.insert(marketCoupon);
        Object o = ResponseUtil.ok(marketCoupon1);
        resp.getWriter().println(JacksonUtil.writeValueAsString(o));
    }
}
